package dev.touchswipe.ScreenLock
{	
	import dev.touchswipe.shared.Animation.Animation;
	import dev.touchswipe.shared.Animation.AnimationEvent;
	
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.geom.Matrix;
	
	[Event(name="complete", type="flash.events.Event")]
	
	public class UnlockSlider extends Sprite
	{
		private var _sliderBtn:UnlockSliderButton;
		private var _touchID:Object;
		
		public function UnlockSlider(){
			_sliderBtn=new UnlockSliderButton();
			addChild(_sliderBtn);
			_sliderBtn.addEventListener(TouchEvent.TOUCH_BEGIN,_onTouchBegin);
			_redraw();
		}
		
		private var _width:Number=280;
		public override function get width():Number{
			return _width;
		}
		public override function set width(value:Number):void{
			_width=value;
			_height=45/280*_width;
			_redraw();
		}
		
		private var _height:Number=45;
		public override function get height():Number{
			return _height;
		}
		public override function set height(value:Number):void{
			_height=value;
			_width=280/45*_height;
			_redraw();
		}
		
		private function _redraw():void{
			graphics.clear();
			var m:Matrix=new Matrix();
			m.createGradientBox(_height,_height,-Math.PI/2,0,0);
			graphics.lineStyle(2,0xFFFFFF,0.2);
			graphics.beginGradientFill(GradientType.LINEAR,[0x232323,0x070707],[1,1],[0,255],m);
			graphics.drawRoundRect(-_height/15,-_height/15,_width+2*_height/15,_height+2*_height/15,27/45*_height);
			_sliderBtn.width=_width/4;
			_sliderBtn.height=_height;
		}
		
		private var _offset:Number=0;
		
		private function _onTouchBegin(event:TouchEvent):void{
			if(_touchID==null){
				_touchID=event.touchPointID;
				stage.addEventListener(TouchEvent.TOUCH_MOVE,_onTouchMove);
				stage.addEventListener(TouchEvent.TOUCH_END,_onTouchEnd);
				
				_offset=event.stageX-_sliderBtn.x;
			}
		}
		
		private function _onTouchMove(event:TouchEvent):void{
			if(_touchID==event.touchPointID){
				var position:Number=event.stageX-_offset;
				if(position<0){
					position=0;
				}
				if(position>3*_width/4){
					position=3*_width/4;
				}
				_sliderBtn.x=position;
			}
		}
		
		private function _onTouchEnd(event:TouchEvent):void{
			if(_touchID==event.touchPointID){
				_touchID=null;
				stage.removeEventListener(TouchEvent.TOUCH_MOVE,_onTouchMove);
				stage.removeEventListener(TouchEvent.TOUCH_END,_onTouchEnd);
				var target:Number;
				var duration:int;
				if(_sliderBtn.x>2*_width/3){
					target=3*_width/4;
					duration=Math.round(10*(target-_sliderBtn.x)/_width);
				}
				else{
					target=0;
					duration=Math.round(10*(_sliderBtn.x-target)/_width);
				}
				var anim:Animation=new Animation(_sliderBtn,"x",_sliderBtn.x,target,duration);
				if(target>0)
					anim.addEventListener(AnimationEvent.STOP_ANIMATION,_unlock);
				anim.start();
			}
		}
		
		private function _unlock(event:AnimationEvent):void{
			dispatchEvent(new Event(Event.COMPLETE));
		}
	}
}